package Page;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ModifyPage {
	
	//修改姓名，地址，电话，更新表格
	
	public void modifyPage(Statement statement) {
		
		JFrame jframe = new JFrame("个人通讯录系统");

		// 当关闭窗口时，退出整个程序
		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// 设置窗口大小
		jframe.setSize(900,300);

		// 显示窗口
		jframe.setVisible(true);

		// 创建面板
		JPanel jpanel=new JPanel();
		jframe.setContentPane(jpanel);
		
		JLabel id_l=new JLabel("请输入将修改联系人的编号:");
		jpanel.add(id_l);
		JTextField id=new JTextField(11);
		jpanel.add(id);
		
		JLabel name_l=new JLabel("请输入修改后联系人的姓名:");
		jpanel.add(name_l);
		JTextField name=new JTextField(11);
		jpanel.add(name);
		
		JLabel address_l=new JLabel("请输入修改后联系人的住址:");
		jpanel.add(address_l);
		JTextField address =new JTextField(11);
		jpanel.add(address);
		
		JLabel tele_l=new JLabel("请输入修改后联系人的号码:");
		jpanel.add(tele_l);
		JTextField tele=new JTextField(11);
		jpanel.add(tele);
		
		JButton button=new JButton("修改该联系人");
		jpanel.add(button);	
	
		button.addActionListener((e)->{
			
			//获得用户输入
			String name_get=name.getText();
			String address_get=address.getText();
			String tele_get=tele.getText();
			String id_get=id.getText();		
		
			
			//根据id修改该联系人，若没有不存在该id的联系人，则添加该id的联系人
			try {
				
				Connection conn = null;
			    PreparedStatement psmt = null;
			    
			    
			    conn = Main.getConnection();
			    String sql = "UPDATE address_book_contact SET name="+name_get+",address="
			    +address_get+",tele="+tele_get+" WHERE id="+id_get+"; ";  
			    psmt = conn.prepareStatement(sql);
			    } catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			//提示信息
			JOptionPane.showMessageDialog(null,"修改编号"+id_get+"的联系人成功！");
		});
		
	}

}
